package com.w3cjava.modules.index;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Cacheable(value = "userCache", key = "#id")
    public User getUserById(String id) {
        // 模拟从数据库中获取数据
        System.out.println("Fetching user from database...");
        return new User(id, "John Doe", 25);
    }

    @CachePut(value = "userCache", key = "#user.id")
    public void saveUser(User user) {
        // 模拟保存用户到数据库
        System.out.println("Saving user to database...");
    }

    @CacheEvict(value = "userCache", key = "#id")
    public void deleteUserById(String id) {
        // 模拟从数据库中删除用户
        System.out.println("Deleting user from database...");
    }
}